package net.openvision.util.xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;

/**
 * Processes xml-elements by delegating the work to a collection of element
 * handlers. When the handling of a new element is requested the first element
 * handler in the list, that supports this element is called.
 * 
 * @author Hannes Widmoser
 * 
 */
public class ElementHandlerCollection extends ArrayList<ElementHandler> implements ElementHandler {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7545890754448421493L;

	@Override
	public Object elementBegin(String elementName, Attributes attributes)
			throws UnknownElementException {
		for (ElementHandler e : this) {
			if (e.isSupported(elementName))
				return e.elementBegin(elementName, attributes);
		}
		throw new UnknownElementException(elementName);
	}

	@Override
	public void elementEnd(String elementName, Object parent)
			throws UnknownElementException {
		for (ElementHandler e : this) {
			if (e.isSupported(elementName)) {
				e.elementEnd(elementName, parent);
				return;
			}
		}
		throw new UnknownElementException(elementName);
	}

	@Override
	public boolean isSupported(String elementName) {
		for (ElementHandler e : this) {
			if (e.isSupported(elementName)) {
				return true;
			}
		}
		return false;
	}

}
